package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Workplan;
@Service
public class WorkplanDAO extends BaseDAO<Workplan, Integer>{
	
	
	public RESULT findByMap(Integer oc,Integer cu, Map<String, String> map, PageBean pb){
		if(oc == null) return null;
		StringBuffer hql = new StringBuffer("FROM Workplan w WHERE w.oc = ? AND w.cu = ?");
		List<Object> list = new ArrayList<Object>();
		
		list.add(oc);
		list.add(cu);
		
		if(map != null){
			String name = map.get(Parm.NAME);
			String deleted = map.get(Parm.DELETED);
			String monthplan = map.get(Parm.GTC);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			
			if(isNotNull(name)){
				hql.append(" AND w.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(monthplan)){
				hql.append(" AND w.monthplan LIKE ?");
				list.add("%" + monthplan + "%");
			}if (isNotNull(deleted)) {
				hql.append(" AND w.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}if(isNotNull(time1)){
				hql.append(" AND w.time1 >= ?");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}if(isNotNull(time2)){
				hql.append(" AND w.time2 <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
		}
		hql.append(" ORDER BY w.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	
	public RESULT findByMap1(List<Integer> ocs, Map<String, String> map, PageBean pb){
		if(ocs == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM Workplan w WHERE w.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer ig : ocs) {
			hql.append("?,");
			list.add(ig);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String name = map.get(Parm.NAME);
			String on = map.get(Parm.OC);
			String deleted = map.get(Parm.DELETED);
			String monthplan = map.get(Parm.GTC);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String suc = map.get(Parm.SUC);
			if(isNotNull(name)){
				hql.append(" AND w.name LIKE ?");
				list.add("%" + name + "%");
			}if (isNotNull(on)) {
				hql.append(" AND w.oc IN (SELECT o.name FROM Organ o WHERE o.name LIKE ?)");
				list.add("%" + on + "%");
			}if(isNotNull(monthplan)){
				hql.append(" AND w.monthplan LIKE ?");
				list.add("%" + monthplan + "%");
			}if (isNotNull(deleted)) {
				hql.append(" AND w.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}if(isNotNull(time1)){
				hql.append(" AND w.time1 >= ?");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}if(isNotNull(time2)){
				hql.append(" AND w.time2 <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}if (isNotNull(suc)) {
				hql.append(" AND w.cu IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY w.code DESC");
		
		return findToPageBean(hql.toString(), pb, list);
	}
	
	
	//导出
	public List<Workplan> findByMap1(List<Integer> ocs, Map<String, String> map){
		if(ocs == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM Workplan w WHERE w.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer ig : ocs) {
			hql.append("?,");
			list.add(ig);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String name = map.get(Parm.NAME);
			String on = map.get(Parm.ON);
			String deleted = map.get(Parm.DELETED);
			String monthplan = map.get(Parm.GTC);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String suc = map.get(Parm.SUC);
			if(isNotNull(name)){
				hql.append(" AND w.name LIKE ?");
				list.add("%" + name + "%");
			}if (isNotNull(on)) {
				hql.append(" AND w.oc IN (SELECT o.name FROM Organ o WHERE o.name LIKE ?)");
				list.add(on);
			}if(isNotNull(monthplan)){
				hql.append(" AND w.monthplan LIKE ?");
				list.add("%" + monthplan + "%");
			}if (isNotNull(deleted)) {
				hql.append(" AND w.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}if(isNotNull(time1)){
				hql.append(" AND w.time1 >= ?");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}if(isNotNull(time2)){
				hql.append(" AND w.time2 <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}if (isNotNull(suc)) {
				hql.append(" AND w.cu IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY w.code DESC");
		
		return find(hql.toString(), list.toArray());
	}
	/**
	 * 根据条件分页查询接收人信息
	 * @param	查询条件
	 * @param	分页对象
	 */
	public RESULT findSubUsers(List<Integer> ocs, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin' AND u.deleted <> 1 AND u.deleted IS NOT NULL AND u.oc IN(");
		List<Object> list =  new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String oc = map.get(Parm.OC);
			String suc = map.get(Parm.SUC);

			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(suc)) {
				hql.append(" AND u.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
}
